package com.personnalManagerSystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.personnalManagerSystem.model.Notice;
import com.personnalManagerSystem.model.User;
import com.personnalManagerSystem.service.INoticeService;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liqing
 * @since 2022-04-05
 */
@Controller
@Slf4j
@RequestMapping("/notice")
public class NoticeController extends BaseController{
    //其他用户模块设计了只能用户查询自己的信息,所以这里需要重写baseList方法
    public  Model baseList(Model model,
                              String option,
                              String searchContent,
                              Integer pageNum,
                              Integer pageSize,
                              INoticeService iNoticeService){
        //设置默认的页面
        if(pageNum==null  || pageNum==0){
            pageNum=1;
        }
        //设置默认的一页显示的记录数
        if(pageSize==null  || pageSize==0){
            pageSize= 5;
        }
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        if(option!=null
                && searchContent!=null
                && !"".equals(option)
                &&!"".equals(searchContent)){
            wrapper.like(option,searchContent);

        }
        /**
         * @Description 在SSM阶段中分页也是利用pageHelper插件,其步骤如下:
         * 1.开启分页
         * pageHelper.startPage(pageNum,pageSize);
         * 2.查询所有的数据
         * mapper.selectAll();
         * 3.将查询的数据封装在pageInfo<T>对象中
         * PageInfo<T>  info = new PageInfo<>(mapper.selectAll())
         * 4.将结果封装在map对象中,然后返回数据
         * 下面这个分页其原理大致相同
         *
         */
        //设置分页
        Page<List<Notice>> pageInfo = PageHelper.startPage(pageNum, pageSize);//返回值是Page类型的数据
        //查询数据
        iNoticeService.list(wrapper);
        System.out.println("pageInfo:"+pageInfo);
        //设置页面信息
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("option",option);
        model.addAttribute("searchContent",searchContent);
        return model;
    }
    @RequestMapping("/list")
    public String list(Model model,
                       String option,
                       String searchContent,
                       Integer pageNum,
                       Integer pageSize){
        baseList(model,option,searchContent,pageNum,pageSize,iNoticeService);
        return "view/notice";

    }
    @RequestMapping("/showNotice")
    @ResponseBody
    /**
     * @Description  公司公告预览
     * @author liqing
     * @data 2022/4/5  20:10
     * @param id
     * @return java.util.Map<java.lang.String,java.lang.Object>
     */
    public Map<String,Object> showNotice(@RequestParam("id")Integer id){
        Map<String,Object>  map = new HashMap<>();
        Notice notice = iNoticeService.getById(id);
        //公告内容
        String content = notice.getContent();
        if(content!=null){
            map.put("content",content);
            map.put("code",200);
            map.put("msg","success");
        }else{
            map.put("content","null");
            map.put("code",201);
            map.put("msg","error");
        }
        return map;
    }

    @RequestMapping("/checkNoticeTitle")
    @ResponseBody
    /**
     * @Description 在添加公告标题之前先检查公共标题的唯一性
     * @author liqing
     * @data 2022/4/6  15:08
     * @param title
     * @return java.util.Map<java.lang.String,java.lang.Object>
     */
    public Map<String,Object> checkNoticeTitle(@RequestParam(value = "title",required = true)String title){
        System.out.println("title:"+title);
        System.out.println("狗狗汪小黄");
        Map<String,Object> map =new HashMap<>();
        Notice notice = iNoticeService.selectNoticeByTitle(title);
        if(notice==null){
            map.put("code",200);
            map.put("msg","该公告标题没有重复");
        }else{
            map.put("code",201);
            map.put("msg","该公告标题已经重复,请重新输入");
        }
        return map;

    }
    @RequestMapping("/add")
    /**
     * @Description  公司公告添加记录
     * @author liqing
     * @data 2022/4/6  14:55
     * @param title
     * @param content
     * @return java.lang.String
     */
    public String add(@RequestParam(value = "title",required = false)String title,
                      @RequestParam(value = "content1",required = false)String content){
        System.out.println("title:"+title);
        System.out.println("content:"+content);
        Notice notice = new Notice();
        notice.setTitle(title);
        //公告添加的时间就是当前时间
        notice.setCreateTime(LocalDateTime.now());
        notice.setContent(content);
        iNoticeService.save(notice);
        return "redirect:/notice/list";
    }

    @RequestMapping("/edit")
    /**
     * @Description   编辑公告
     * @author liqing
     * @data 2022/4/6  18:25
     * @param id
     * @param title
     * @param content
     * @return java.lang.String
     */
    public String edit(@RequestParam(value = "id",required = true)Integer id,
                       @RequestParam(value = "title2",required = false)String title,
                       @RequestParam(value = "content2",required = false)String content){
        Notice notice = new Notice();
        notice.setId(id);
        notice.setTitle(title);
        notice.setContent(content);
        iNoticeService.saveOrUpdate(notice);
        return "redirect:/notice/list";
    }


    @RequestMapping("/del")
    public String del(@RequestParam(value = "id",required = true)Integer id){
        System.out.println("id:"+id);
        iNoticeService.removeById(id);
        return "redirect:/notice/list";
    }

}

